package StudyCode.summary.mycollection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

/**
 * 版权所有 科技与人文(www.tah1986.com)
 */
public class CreatesACollectionFromAnIterable {
	public static <T> Collection<T> asCollection(Iterable<T> iterable) {
		List<T> list = new ArrayList<T>();
		addToCollection(iterable, list);
		return list;
	}


	public static <C extends Collection<T>, T> C addToCollection(
			Iterator<T> iterator, C collection) {
		while (iterator.hasNext()) {
			collection.add(iterator.next());
		}
		return collection;
	}


	public static <C extends Collection<T>, T> C addToCollection(
			Iterable<T> iterable, C collection) {
		return addToCollection(iterable.iterator(), collection);
	}
}
